package com.lenovo.smarttraffic.ui.activity.stage3;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.ui.adapter.MyVpAdapter;
import com.lenovo.smarttraffic.ui.fragment.stage3.RealTimeFragment;

import java.util.ArrayList;
import java.util.List;

public class RealTimeActivity extends BaseActivity {

    private List<Fragment> fragments = new ArrayList<>();
    private ViewPager viewPager;
    private MyVpAdapter adapter;

    private SharedPreferences preferences;

    private LinearLayout indicate;

    private int position = 0;

    @Override
    protected int getLayout() {
        return R.layout.activity_real_time;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            position = (int) bundle.get("pos");
        }

        preferences = getSharedPreferences(Constant.ACCOUNT, MODE_PRIVATE);

        initView();
        initData();
    }

    public void initView() {
        initToolBar(this, "实时显示");

        viewPager = findViewById(R.id.view_pager);
        indicate = findViewById(R.id.env_indicate);
    }

    public void initData() {
        fragments.add(new RealTimeFragment().setType("温度"));
        fragments.add(new RealTimeFragment().setType("湿度"));
        fragments.add(new RealTimeFragment().setType("光照"));
        fragments.add(new RealTimeFragment().setType("CO2"));
        fragments.add(new RealTimeFragment().setType("PM2.5"));
        fragments.add(new RealTimeFragment().setType("道路状态"));


        adapter = new MyVpAdapter(getSupportFragmentManager(), fragments);
        viewPager.setOffscreenPageLimit(fragments.size());
        viewPager.setAdapter(adapter);

        for (int i = 0; i < fragments.size(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setPadding(10, 0, 10, 0);
            if (i == 0) {
                imageView.setImageResource(R.drawable.indicate_selected);
            } else {
                imageView.setImageResource(R.drawable.indicate);
            }
            imageView.setTag(i);
            indicate.addView(imageView);
        }

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < fragments.size(); i++) {
                    ImageView imageView = indicate.findViewWithTag(i);
                    if (i == position) {
                        imageView.setImageResource(R.drawable.indicate_selected);
                    } else {
                        imageView.setImageResource(R.drawable.indicate);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        viewPager.setCurrentItem(position);

    }

}
